package com.example.demo.service.impl;

import com.example.demo.entity.Activity;
import com.example.demo.entity.Joiner;
import com.example.demo.pojo.PlannerResult;
import com.example.demo.repository.ActivityRepository;
import com.example.demo.repository.JoinerRepository;
import com.example.demo.repository.UserRepository;
import com.example.demo.service.HomeService;
import com.example.demo.utils.ThreadLocalUtil;
import com.example.demo.vo.JoinerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;


@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    JoinerRepository joinerRepository;

    @Autowired
    ActivityRepository activityRepository;

    @Autowired
    UserRepository userRepository;

    //加入活动
    @Override
    public PlannerResult<String> join_act(JoinerVo joinerVo) {
        Joiner joiner=new Joiner();
        Map<String,Object> user = ThreadLocalUtil.get();
        Integer userid = (Integer) user.get("id");

        joiner.setUser_id(userid);
        joiner.setIdentity("嘉宾");
        joiner.setQuanxian("参与者");
        joiner.setActivity_id(joinerVo.getActivity_id());

        Integer sum = 0;
        List<Joiner> joiners = joinerRepository.findByActivity_id(joinerVo.getActivity_id());
        sum = joiners.size();

        Activity activity = activityRepository.findById(joinerVo.getActivity_id()).get();
        if(activity.getState().equals("报名中"))
        {

            if(sum>=activity.getMax_num())
                return PlannerResult.error("报名人数已达上限");
            else {
                Joiner result = joinerRepository.save(joiner);

                if(result.getId()!=null){
                    activity.setJoiner_number(activity.getJoiner_number()+1);
                    activityRepository.save(activity);
                    return PlannerResult.success("加入活动成功");
                }
                else
                    return PlannerResult.error("加入活动失败");
            }

        }
        else
        {
            return PlannerResult.error("报名失败，活动"+activity.getState());
        }

    }

    //退出活动
    @Override
    public PlannerResult<String> exit_act(Integer userid, Integer actid) {
        joinerRepository.deleteByActivity_idAndUser_id(actid,userid);
        return PlannerResult.success("已退出");
    }


    //加入的活动

    @Override
    public PlannerResult<List<Activity>> my_act_join(Integer userid) {
        List<Joiner> lists= joinerRepository.findByUser_id(userid);
        List<Activity> listA = null;

        for (Joiner list: lists
             ) {
            listA.add(activityRepository.findById(list.getActivity_id()).get());
        }
        return PlannerResult.success(listA);
    }

    //创建的活动

    @Override
    public PlannerResult<List<Activity>> my_act_create(Integer userid) {
        List<Activity> list=activityRepository.findByCreator(userid);
        return PlannerResult.success(list);
    }

}
